package com.ruoyi.project.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.project.domain.dto.CanshuDTO;
import com.ruoyi.project.domain.entity.Canshu;
import com.ruoyi.project.mapper.CanshuMapper;
import com.ruoyi.project.service.CanshuService;
import jodd.util.ArraysUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * (Canshu)表服务实现类
 *
 * @author makejava
 * @since 2025-06-29 16:17:34
 */
@Service
@AllArgsConstructor
public class CanshuServiceImpl extends ServiceImpl<CanshuMapper, Canshu> implements CanshuService {

    protected LambdaQueryWrapper<Canshu> queryWrapper(CanshuDTO param) {
        return new LambdaQueryWrapper<Canshu>()
            .like(StrUtil.isNotEmpty(param.getKeyword()), Canshu::getParamType, param.getKeyword())
            .eq(ObjectUtil.isNotNull(param.getSpecial()), Canshu::getSpecial, param.getSpecial())
            .eq(ObjectUtil.isNotNull(param.getStatus()), Canshu::getStatus, param.getStatus())
            .orderByDesc(Canshu::getId);
    }

    @Override
    public Page<?> listPage(Page<Canshu> page, CanshuDTO param) {
        return this.page(page, queryWrapper(param));
    }

    @Override
    public List<Canshu> queryList(CanshuDTO param) {
        return this.list(queryWrapper(param));
    }

    @Override
    public boolean saveOrUpdate(CanshuDTO param) {
        return param.isNew() ? this.save(param) : this.updateById(param);
    }

    @Override
    public boolean disabled(Integer id) {
        return this.update(new LambdaUpdateWrapper<Canshu>().set(Canshu::getStatus, false).eq(Canshu::getId, id));
    }

    @Override
    public Map<String, List<String>> getDictData() {
        List<Canshu> list = this.list(queryWrapper(CanshuDTO.buildStatus(true)));
        return new HashMap<String, List<String>>() {{
            for (Canshu d : list) {
                if (StrUtil.isAllNotEmpty(d.getParamCode(), d.getParamItem()) && d.getStatus()) {
                    if (!d.getSpecial()) {
                        put(d.getParamCode(), StrUtil.split(d.getParamItem(), ",").stream().distinct().collect(Collectors.toList()));
                    } else if (JSONUtil.isTypeJSONArray(d.getParamItem()) && JSONUtil.isTypeJSONArray(d.getParamBelows())) {
                        System.out.println("1 " + d.getParamType());
                        String sep = "_{}_";
                        Map<String, String> data = new LinkedHashMap<>();
                        data.put(d.getParamCode(), d.getParamCode());
                        for (Map<String, String> m : JSONUtil.toList(d.getParamItem(), Map.class)) {
                            for (String k : m.keySet()) {
                                data.put(k, k);
                                data.put(k, StrUtil.join(sep, data.keySet()));
                            }
                        }
                        Map<String, Set<String>> temp = new LinkedHashMap<>();
                        for (Map<String, String> obj : JSONUtil.toList(d.getParamBelows(), Map.class)) {
                            int idx = 0;
                            for (String k : data.keySet()) {
                                if (idx == 0) {
                                    Set<String> set = temp.containsKey(k) ? temp.get(k) : new LinkedHashSet<>();
                                    set.add(obj.get(k));
                                    temp.put(k, set);
                                } else if (StrUtil.isNotEmpty(obj.get(k))) {
                                    String keys = data.get(k);
                                    for (String key : StrUtil.split(keys, sep)) {
                                        if (Objects.equals(key, k)) {
                                            break;
                                        }
                                        for (String item : StrUtil.split(obj.get(key), ",")) {
                                            keys = StrUtil.format(keys, item);
                                            if (!StrUtil.contains(keys, sep)) {
                                                Set<String> set = temp.containsKey(keys) ? temp.get(keys) : new LinkedHashSet<>();
                                                set.addAll(StrUtil.split(obj.get(k), ","));
                                                temp.put(keys, set);
                                            }
                                        }
                                    }
                                }
                                idx++;
                            }
                        }
                        temp.forEach((k, v) -> put("SPECIAL_" + k, new ArrayList<>(v)));
                    }
                }
            }
        }};
    }
}
